import {ipcRenderer, contextBridge} from 'electron'

// --------- Expose some API to the Renderer process ---------
contextBridge.exposeInMainWorld('ipcRenderer', {
    on(...args: Parameters<typeof ipcRenderer.on>) {
        const [channel, listener] = args
        return ipcRenderer.on(channel, (event, ...args) => listener(event, ...args))
    },
    off(...args: Parameters<typeof ipcRenderer.off>) {
        const [channel, ...omit] = args
        return ipcRenderer.off(channel, ...omit)
    },
    send(...args: Parameters<typeof ipcRenderer.send>) {
        const [channel, ...omit] = args
        return ipcRenderer.send(channel, ...omit)
    },
    invoke(...args: Parameters<typeof ipcRenderer.invoke>) {
        const [channel, ...omit] = args
        return ipcRenderer.invoke(channel, ...omit)
    },

    // You can expose other APTs you need here.
    // ...
    // 窗口控制
    setTitle: (title: string) => ipcRenderer.invoke('set-title', title),
    minimizeWindow: () => ipcRenderer.invoke('minimize-window'),
    maximizeWindow: () => ipcRenderer.invoke('maximize-window'),
    closeWindow: () => ipcRenderer.invoke('close-window'),

    // 菜单事件监听
    onMenuNewFormula: (callback: () => void) =>
        ipcRenderer.on('menu-new-formula', callback),
    onMenuSaveFormula: (callback: () => void) =>
        ipcRenderer.on('menu-save-formula', callback),
    onMenuOpenOCR: (callback: () => void) =>
        ipcRenderer.on('menu-open-ocr', callback),
    onMenuOpenLibrary: (callback: () => void) =>
        ipcRenderer.on('menu-open-library', callback),

    // 移除监听器
    removeAllListeners: (channel: string) =>
        ipcRenderer.removeAllListeners(channel)
})
